/**
 * 
 */
package GUI.GameBoard.Center;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import Utils.Utils;

//VS4E -- DO NOT REMOVE THIS LINE!
public class BankPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private String txtCashier;
	private String imgPath;
	private JPanel pnlPicture;
	private final String header = "Bank Cashier:\n";
	private JLabel lblImage;


	public BankPanel() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		// Make transparent
		setOpaque(false);
		add(getPnlPicture("/cash.png",100000.0));
		setPreferredSize(new Dimension(100,98));
	}

	/**
	 * Create the Picture Panel
	 * @return The Picture Panel
	 */
	private JPanel getPnlPicture(String imagePath, Double cash)
	{
		if(pnlPicture == null)
		{
			// Save the Cash Amount
			txtCashier = Double.toString(cash);
			
			// Save the Image Path
			imgPath = imagePath;
			// Create a new Panel with Border LAyout
			pnlPicture = new JPanel(new BorderLayout());
			// Make transparent
			pnlPicture.setOpaque(false);
			// Get The Image
			ImageIcon image = Utils.getImageIcon(imagePath);
			// Check if the Image Exists
			if(image != null)
			{
				// Set the Preferred Size by the Image
				pnlPicture.setPreferredSize(new Dimension(image.getIconWidth(),image.getIconHeight()));
				lblImage = getImageLabel(image, txtCashier);
				// Add the Image to the Panel
				pnlPicture.add(lblImage, BorderLayout.CENTER);
				// Add a Component Resize Listener
				pnlPicture.addComponentListener(new ResizeListener());
			}
		}
		return pnlPicture;
	}
	
	private JLabel getImageLabel(ImageIcon image, String text)
	{
		int fontSize = 12;
//		if(pnlPicture.getHeight() > 0)
//			fontSize = pnlPicture.getHeight()/2;
		// Set a Font for the Label
		Font font = new Font("Verdana", Font.BOLD, fontSize);
		// Create an Image Label
		JLabel lblImage = new JLabel(image, JLabel.CENTER);
		// Add the Cash Amount Text
		lblImage.setText(Utils.StringToHTML(header + text));
		// Align the Text the The Center
		lblImage.setAlignmentY(CENTER_ALIGNMENT);
		// Set the Text Font
		lblImage.setFont(font);
		
		return lblImage;
	}
	
	/**
	 * Returns the Image Path
	 * @return The Image Path
	 */
	private String getImagePath()
	{
		return imgPath;
	}
	
	/**
	 * Update the Cash Amount
	 * @param cash The Amount of Cash Available
	 */
	public void setCash(Double cash)
	{
		txtCashier = Double.toString(cash);
		lblImage.setText(Utils.StringToHTML(header + txtCashier));
	}

	/**
	 * Resize listener to ensure image is sized correctly for the panel
	 *
	 */
	class ResizeListener extends ComponentAdapter {

		/**
		 * Overrides ComponentAdapter.componentResized
		 * Ensures the image is correctly resized
		 * @param evt the event
		 */
		public void componentResized(ComponentEvent evt) {
			// Notify that the Component Resized
			super.componentResized(evt);
			// Check if this Component is Our Panel
			if(evt.getComponent() instanceof JPanel) {
				// Clear the Panel
				pnlPicture.removeAll();
				// Create a new Resized Image	
				ImageIcon resizedIcon = Utils.createScaledIcon(getImagePath(), pnlPicture.getWidth(), pnlPicture.getHeight(), pnlPicture.getInsets());
				// Recreate the Picture Panel
				lblImage = getImageLabel(resizedIcon, txtCashier);
				pnlPicture.add(lblImage, BorderLayout.CENTER);
				// Repaint and Validate
				Utils.repaintAndValidateAll(BankPanel.this);
			}
		}
	}


	public static void main(String[] args) {
		BankPanel bp = new BankPanel();
		JFrame frame = new JFrame();
		frame.getContentPane().setBackground(null);
		frame.getContentPane().add(bp);
		frame.setSize(bp.getPreferredSize());
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}
